package com.crm.service.impl.performanceRankingManager;

import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerPayCustomMapper;
import com.crm.dao.mapper.customMapper.performanceRankingManager.PerformanceRankingMapper;
import com.crm.dao.mapper.customMapper.sys.FileUploadCustomerMapper;
import com.crm.dao.mapper.sys.FileUploadMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.performanceRankingManager.PerformanceRankingQO;
import com.crm.model.qo.sys.UserAvatar;
import com.crm.model.vo.customerManager.CustomerPayMoneyVO;
import com.crm.model.vo.performanceRankingManager.*;
import com.crm.service.performanceRankingManager.PerformanceRankingDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ Author ：pym
 * @ Date   ：2021/8/27 11:05
 */
@Service
public class PerformanceRankingDbServiceImpl implements PerformanceRankingDbService {
    @Autowired
    PerformanceRankingMapper performanceRankingMapper;
    @Autowired
    FileUploadMapper fileUploadMapper;
    @Autowired
    FileUploadCustomerMapper fileUploadCustomerMapper;
    @Autowired
    CustomerPayCustomMapper customerPayCustomMapper;

    /**
     * 个人业绩统计
     *
     * @param performanceRankingQO 参数
     * @return {@link {java.util.List<com.crm.model.vo.performanceRankingManager.PersonalPerformanceRankingVO>}}
     * @Author pym
     * @Date 2021/8/27 14:13
     */
    @Override
    public List<PersonalPerformanceRankingVO> personalPerformanceRanking(PerformanceRankingQO performanceRankingQO) {
        return performanceRankingMapper.personalPerformanceRanking(performanceRankingQO);
    }

    /**
     * 公司业绩统计
     *
     * @param performanceRankingQO 参数
     * @return {@link {java.util.List<com.crm.model.vo.performanceRankingManager.CompanyPerformanceRankingVO>}}
     * @Author pym
     * @Date 2021/8/27 14:13
     */
    @Override
    public List<CompanyPerformanceRankingVO> companyPerformanceRanking(PerformanceRankingQO performanceRankingQO) {
        return performanceRankingMapper.companyPerformanceRanking(performanceRankingQO);
    }

    /**
     * 公司业绩统计
     */
    @Override
    public List<CompanyPerformanceRankingYmVO> companyPerformanceRankingByYm(String dataStr) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        List<CompanyPerformanceRankingYmVO> companyPerformanceRankingYmVOS =
                performanceRankingMapper.companyPerformanceRankingByYm(dataStr, userAccount.getCompanyId());
//                performanceRankingMapper.companyPerformanceRankingByYm(dataStr, "1da88df8a7db11eb9e0a00163e08893d");
        if (companyPerformanceRankingYmVOS == null || companyPerformanceRankingYmVOS.size() == 0) {
            return new ArrayList<>();
        }

        //取出所有业务员Id，用于查询充值表充值金额
        List<String> accountIds = companyPerformanceRankingYmVOS.stream().map(CompanyPerformanceRankingYmVO::getAccountId).collect(Collectors.toList());
        if (accountIds.size() > 0) {
            List<CustomerPayMoneyVO> payMoneyByAccountId = customerPayCustomMapper.getPayMoneyByAccountId(accountIds, dataStr);
            for (CustomerPayMoneyVO payMoneyVO : payMoneyByAccountId) {
                for (CompanyPerformanceRankingYmVO vo : companyPerformanceRankingYmVOS) {
                    //如果 当前业务员本月办理了充值业务，则将充值金额添加到该业务员的业绩中
                    if (payMoneyVO != null && payMoneyVO.getPayMoney() != null  && payMoneyVO.getAccountId().equals(vo.getAccountId()) && payMoneyVO.getPayMoney().doubleValue() > 0) {
                        vo.setArriveMoney(vo.getArriveMoney().add(payMoneyVO.getPayMoney()));
                    }
                }
            }
        }

        // 用户头像获取
        List<String> avatarIds = companyPerformanceRankingYmVOS.stream().map(CompanyPerformanceRankingYmVO::getAvatar).collect(Collectors.toList());
        if (avatarIds.size() > 0) {
            List<UserAvatar> userAvatar = fileUploadCustomerMapper.getUserAvatar(avatarIds);
            for (UserAvatar user : userAvatar) {
                for (CompanyPerformanceRankingYmVO vo : companyPerformanceRankingYmVOS) {
                    if (user.getUploadId().equals(vo.getAvatar())) {
                        vo.setAvatar(user.getAccessUrl());
                    }
                }
            }
        }

        List<CompanyPerformanceRankingYmVO> collect = companyPerformanceRankingYmVOS.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        return collect;
    }

    /**
     * 产品占比
     */
    @Override
    public List<AnalysisOfProductProportionVO> getAnalysisOfProductProportion(String dataStr, String accountId) {

        List<AnalysisOfProductProportionVO> analysisOfProductProportion = performanceRankingMapper.getAnalysisOfProductProportion(dataStr, accountId);

        List<AnalysisOfProductProportionVO> collect = analysisOfProductProportion.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        if (collect.size() > 5) {
            List<AnalysisOfProductProportionVO> analysisOfProductProportionVOS = collect.subList(0, 4);
            double temp = 0;

            for (AnalysisOfProductProportionVO vo :
                    analysisOfProductProportionVOS) {
                temp += vo.getRatio();
            }
            AnalysisOfProductProportionVO other = new AnalysisOfProductProportionVO("其他", "", "", 1 - temp);
            analysisOfProductProportionVOS.add(other);
            return analysisOfProductProportionVOS;
        } else {
            return collect;

        }
    }

    /**
     * 个人业绩组成
     */
    @Override
    public List<PerformanceComposition> getPerformanceComposition(String dataStr, String accountId) {
        return performanceRankingMapper.getPerformanceComposition(dataStr, accountId);
    }


}
